package cn.icare.cms.impl;

import cn.icare.cms.LimitItemService;
import cn.icare.cms.ListUtil;
import cn.icare.cms.dao.mapper.CustomMapper;
import cn.icare.cms.dao.mapper.ItemCatMapper;
import cn.icare.cms.dao.mapper.LimitItemMapper;
import cn.icare.cms.dao.po.ItemCat;
import cn.icare.cms.dao.po.LimitItem;
import cn.icare.cms.model.vo.rest.MsgDatasVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class LimitItemServiceImpl implements LimitItemService {


    @Resource
    private LimitItemMapper mapper;

    @Resource
    private CustomMapper customMapper;

    private final Logger logger = LoggerFactory.getLogger(LimitItemServiceImpl.class);

    @Override
    public Long createOne(LimitItem ro) {

        int result = mapper.insertSelective(ro);
        if (result > 0)
            return ro.getItemid();

        return 0L;
    }

    @Override
    public boolean updateOne(LimitItem ro) {
        return mapper.updateByPrimaryKey(ro) > 0;
    }

    @Override
    public boolean deleteOne(Long itemId) {

        return mapper.deleteByPrimaryKey(itemId) > 0;
    }

    @Override
    public boolean deleteBatch(List<Long> itemIds) {
        boolean result = false;
        try {
            if (ListUtil.isNotEmpty(itemIds)) {
                Example example = new Example(LimitItem.class);
                Example.Criteria criteria = example.createCriteria();
                criteria.andIn("itemid", itemIds);
                result = this.mapper.deleteByExample(example) > 0;
            }
        } catch (Exception e) {
            this.logger.error("deleteBatch 异常", e);
        }
        return result;
    }

    @Override
    public LimitItem getOne(Long itemId) {
        return mapper.selectByPrimaryKey(itemId);
    }

    @Override
    public MsgDatasVO getList() {

        String sql = "select a.*,b.title from t_limititem a,t_item b where a.itemid=b.id";

        MsgDatasVO result = new MsgDatasVO();
        try {

            List<Map<String, Object>> dataList = this.customMapper.select(sql);
            result.setTotal(dataList.size());
            result.setDatas(dataList);

        } catch (Exception ex) {
            this.logger.error("getList 异常", ex);

        }
        return result;
    }

    @Override
    public boolean isExists(Long itemId) {
        return mapper.existsWithPrimaryKey(itemId);
    }

    @Override
    public boolean checkItemIsLimitShop(LimitItem limitItem, Long memberId) {
        String sql = "select count(1)  from t_order_item i,t_order o where i.orderid=o.id and o.status<>6";
        sql += " and i.itemid=" + limitItem.getItemid() + " and o.memberid=" + memberId + " and createtime>=ADDDATE(SYSDATE(),INTERVAL -" + limitItem.getTimenum() + " DAY)";
        int result = customMapper.selectCount(sql);
        return result > 0;
    }
}
